<?php

if( !defined("IN_ELCIUS") ) exit;


function ProcessTemplate( $html, &$data ) {
	
	return ProcessTemplateBlock( $html, $data );
	
}

function ProcessTemplateBlock( $html, &$data, &$gdata = null ) {
	
	//echo $html."\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	
	if( !(is_string( $html ) or is_numeric( $html ) ) ) return ''; // TODO: template processing error, trying to print array
	
	// locate blocks in html
	preg_match_all(
		"/<!--[\s]*(LOOP|IF)[\s]+([a-z0-9_\^-]+)[\s]+([a-z0-9_\-\!\*\,\s]*)[\s]+-->(.*?)<!--[\s]*END \\2[\s]*-->/is",
		$html,
		$blocks,
		PREG_OFFSET_CAPTURE
	);
	
	$return = "";
	
	// process blocks
	$last_offset = 0;
	foreach( $blocks[0] as $i => $area ) {
		// process static html located before block
		$return .= ReplaceTags( substr( $html, $last_offset, $area[1] - $last_offset ), $data, $gdata );
		$last_offset = $area[1] + strlen( $area[0] );
		
		// confirm block has associated data
		$blockTagData = GetTagValue( $blocks[2][ $i ][0], $data, $gdata );
		
		if( isset( $blockTagData ) ) {
			
			if( $blocks[1][ $i ][0] == "LOOP" ) {
				
				if( is_array( $blockTagData ) ){
					
					// calculate the range
					if( preg_match( '/([\-]*[0-9]+|[\*])\,[\s]*([\-]*[0-9]+|[\*])/', $blocks[3][ $i ][0], $range ) ){
						list( $start, $end ) = CalcLoopRange( $range[1], $range[2], count( $blockTagData ) );
					}
					else{
						$start = 0;
						$end = count( $blockTagData );
					}
					
					// process the loop
					$j = 0;
					foreach( $blockTagData as $areaData ) {
						if( $j >= $start && $j < $end ) {
							if( !is_array($areaData) ){
								$areaData = array($areaData);
							}
							$areaData['i'] = $j-$start; // counter for loots, TODO: add even/odd?
							$return .= ProcessTemplateBlock( $blocks[4][$i][0], $areaData, $data );
						}
						$j++;
					}
					
				}
			}
			elseif( $blocks[1][ $i ][0] == "IF" ){
				
				// convert booleans to int
				if( is_bool( $blockTagData ) ){
					$blockTagData = $blockTagData ? 1 : 0;
				}
				
				if( is_numeric( $blockTagData ) or is_string( $blockTagData ) ){
					
					if( $blockTagData == $blocks[3][ $i ][0] ){
						$return .= ProcessTemplateBlock( $blocks[4][$i][0], $data, $gdata );
					}
					
				}
				
			}
		}
	}
	
	// process remaining static html (entire file if no blocks)
	$return .= ReplaceTags( substr( $html, $last_offset ), $data, $gdata );
	
	return $return;
	
}

function ReplaceTags( $html, &$data, &$gdata ){
	
	return preg_replace_callback(
		'/{([a-z0-9_\^-]+)}/i',
		function($m) use (&$data,&$gdata){
			return ProcessTemplateBlock( GetTagValue( $m[1], $data, $gdata ), $data, $gdata );
		},
		$html
	);
}

function GetTagValue( $tag, &$data, &$gdata ){
	$tag = str_replace( '-','"]["', Clean($tag,'a-zA-Z0-9_\^-') ); // TODO: clean string manually for this, very risky as is.
	
	if( $tag{0} == '^' ){
		$tag = substr( $tag, 1 );
		if( isset( $gdata ) ){
			return FormatTagValue( eval('return isset( $gdata["'.$tag.'"] ) ? $gdata["'.$tag.'"] : null;') );
		}
	}
	return FormatTagValue( eval('return isset( $data["'.$tag.'"] ) ? $data["'.$tag.'"] : null;') );
}

function CalcLoopRange( $s, $e, $n ){
	if( $s == '*' ) $s = $n;
	if( $e == '*' ) $e = $n;
	if( $s < 0 ) $s += $n;
	if( $e < 0 ) $e += $n;
	return array($s,$e);
}

function FormatTagValue( $v ){
	if( is_bool( $v ) ){
		return $v ? 'True' : 'False';
	}
	if( is_null( $v ) ){
		return 'Null';
	}
	return $v;
}



function GetRequestVar( $var ){
	if( isset( $_GET[ $var ] ) ){
		return $_GET[ $var ];
	}
	if( isset( $_POST[ $var ] ) ){
		return $_POST[ $var ];
	}
}




?>